package jsyf.oa.service.system;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import jsyf.com.util.DateUtils;
import jsyf.oa.dao.system.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import jsyf.oa.dao.wechat.service_company.TokenDao;
import jsyf.oa.entity.system.UserEntity;
import jsyf.oa.entity.system.UserLeaveEntity;
import jsyf.oa.facade.system.UserLeaveFacade;
import redis.clients.jedis.JedisCluster;

/**
 * Created by Administrator on 2018/1/10 0010.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class UserLeaveService implements UserLeaveFacade {
    @Autowired
    private UserLeaveDao userleaveDao;
    @Autowired
    private UserDao userDao;

    @Autowired
    private TokenDao tokenDao;

    @Autowired
    private SignInDao signInDao;
    @Autowired
    private LeaveDao leaveDao;
    @Autowired
    private TargetDao targetDao;

    /* @Autowired
     private JedisCluster jedisCluster;*/
    @Override
    public String save(String jsonData) {
        UserLeaveEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), UserLeaveEntity.class);
        //根据userId删除离职信息
        userleaveDao.removeByCondition(" AND USER_ID=?", new String[]{entity.getUserId()});
        Integer rows = userleaveDao.add(entity);
        if (entity.getUser() != null) {
            UserEntity oldUser = (UserEntity) userDao.getIBaseEntityById(entity.getUser().getId());

            List<UserEntity> entityList =
                    userDao.getEntityListByCondition("AND PARENT_ID = ? AND JOB_STATE != 1",new Object[]{entity.getUser().getId()});
            for (int i = 0; i < entityList.size(); i++) {
                UserEntity userEntity = entityList.get(i);
                userEntity.setParentId(oldUser.getParentId());
                userDao.update(userEntity);
            }

            oldUser.setJobState(entity.getUser().getJobState());
            oldUser.setDelFlag(1);
            oldUser.setParentId("");
            rows = userDao.update(oldUser);
            //清除radis中的缓存数据
            String tel = oldUser.getTel();
            String pwd = oldUser.getPwd();
           /* if (StringUtils.isNotEmpty(tel) && StringUtils.isNotEmpty(pwd)){
                if (jedisCluster.exists("tel_"+tel) && jedisCluster.exists("pwd_"+tel+pwd)){
                    jedisCluster.del("tel_"+tel);
                    jedisCluster.del("pwd_"+tel+pwd);
                    jedisCluster.del("result_" + tel + pwd);
                }
            }*/

            //企业号: 人员离职
            tokenDao.leave(oldUser.getId());
        }
        //离职后删除此人目标
        targetDao.removeByCondition(" AND USER_ID = ? ", new String[]{entity.getUserId()});
        //离职后如果当天签到删除当天签到记录
        Calendar c = Calendar.getInstance();
        String nowTime = DateUtils.parse(c.getTime(), "yyyy-MM-dd");
        String weekTime = DateUtils.getAfterWeek(c.getTime());
        signInDao.removeByCondition(" AND USER_ID =? AND SIGN_DATE=? ", new String[]{entity.getUserId(), nowTime});
        leaveDao.removeByCondition(" AND USER_ID =? AND LEAVE_DATE BETWEEN ? AND ?  ", new String[]{entity.getUserId(), nowTime, weekTime});
        return rows == 0 ? "" : entity.getId();
    }

    @Override
    public List<JSONObject> getLeaveByUserId(String userId) {
        List<JSONObject> userLeave = new ArrayList<>();
        JSONObject userObj = userDao.getJSONById(userId);
        if ("1".equals(userObj.getString("jobState"))) {
            userLeave = userleaveDao.getJSONListByCondition(" and USER_ID=?",
                    new Object[]{userId});
        }
        return userLeave;
    }
}
